package com.itcast.activiti.controller;

import cn.itcast.ic.common.bean.Response;
import com.itcast.activiti.domain.AddSign;
import com.itcast.activiti.domain.RemoveSign;
import com.itcast.activiti.domain.TaskStart;
import com.itcast.activiti.service.ITaskRuntimeService;
import com.itcast.activiti.utils.ActivitiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 任务相关接口
 */

@RestController
@RequestMapping("/task")
public class TaskController {
    @Autowired
    private ITaskRuntimeService iTaskRuntimeService;

    /**
     * 获取当前任务
     *
     * @return 任务list
     */
    @PostMapping(value = "/list")
    public Response listTaskPage(@RequestBody ActivitiPage activitiPage) {
        return Response.success(iTaskRuntimeService.listTaskPage(activitiPage));
    }

    /**
     * 代理人任务
     *
     * @param assignee    经办人（当前用户）
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @return
     */
    @GetMapping(path = "/{businessKey}")
    public Response findTaskByAssignee(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String assignee, @RequestParam(required = false) String tenantId) {
        return Response.success(iTaskRuntimeService.listMapTask(iTaskRuntimeService.listTask(tenantId, businessKey, assignee)));
    }

    /**
     * 任务审批
     *
     * @param assignee    经办人
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @param taskStart   意见、map变量
     * @param applyType   1：审批（默认））2：驳回到发起人）3：驳回上一级）
     * @return
     */
    @PostMapping(path = "/apply/{businessKey}/{assignee}")
    public Response completeTaskApply(@PathVariable("businessKey") String businessKey,
                                      @PathVariable("assignee") String assignee,
                                      @RequestParam(required = false) Integer applyType,
                                      @RequestBody TaskStart taskStart) {
        if (applyType == null) {
            if (taskStart.getApplyType() != null) {
                applyType = taskStart.getApplyType();
            }
        }
        return Response.success(iTaskRuntimeService.completeTaskApply(StringUtils.hasText(taskStart.getTenantId()) ? taskStart.getTenantId() : "PMIS", businessKey, assignee, taskStart, applyType));
    }


    /**
     * 任务审批(自动完成审批人相同的后面节点)
     *
     * @param assignee    经办人
     * @param businessKey 业务标识，通常为业务表的主键，业务标识和流程实例一一对应。
     * @param taskStart   意见、map变量
     * @param applyType   1：审批（默认））2：驳回到发起人）3：驳回上一级）
     * @return
     */
    @PostMapping(path = "/apply/auto/next/{businessKey}/{assignee}")
    public Response completeTaskApplyAutoNext(@PathVariable("businessKey") String businessKey,
                                              @PathVariable("assignee") String assignee,
                                              @RequestParam(required = false) Integer applyType,
                                              @RequestBody TaskStart taskStart) {
        if (applyType == null) {
            if (taskStart.getApplyType() != null) {
                applyType = taskStart.getApplyType();
            }
        }
        return Response.success(iTaskRuntimeService.autoNextCompleteTaskApply(StringUtils.hasText(taskStart.getTenantId()) ? taskStart.getTenantId() : "PMIS", businessKey, assignee, taskStart, applyType, null));
    }

    /**
     * 转交/归还
     *
     * @param businessKey
     * @param assignee
     * @param assigneeOther
     * @return
     */
    @GetMapping(path = "/transfer/{businessKey}/{assignee}")
    public Response transferTask(@PathVariable("businessKey") String businessKey, @PathVariable("assignee") String assignee, @RequestParam(required = false) String assigneeOther, @RequestParam(required = false) String tenantId) {
        return Response.success(iTaskRuntimeService.transferTask(tenantId, businessKey, assignee, assigneeOther));
    }

    /**
     * 任务委托
     * 将任务委托给另一个用户。这意味着已设置受让人，并将委派状态设置为DelegationState.PENDING。如果没有在任务上设置所有者，则所有者将被设置为任务的当前受让人。
     *
     * @param businessKey
     * @param assignee          当前代理人
     * @param delegatedAssignee 将被设置为受让人的用户
     * @return
     */
    @GetMapping(path = "/delegated/{businessKey}")
    public Response delegatedTask(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String assignee, @RequestParam(required = false) String delegatedAssignee, @RequestParam(required = false) String tenantId) {
        return Response.success(iTaskRuntimeService.delegatedTask(tenantId, businessKey, assignee, delegatedAssignee));
    }

    /**
     * 加签
     *
     * @param businessKey
     * @param assignee
     * @param addSign
     * @return
     */
    @PostMapping(path = "/add/sign/{businessKey}/{assignee}")
    public Response addSign(@PathVariable("businessKey") String businessKey, @PathVariable("assignee") String assignee, @RequestBody AddSign addSign) {
        return Response.success(iTaskRuntimeService.addSign(StringUtils.hasText(addSign.getTenantId()) ? addSign.getTenantId() : "PMIS", businessKey, assignee, addSign));
    }

    /**
     * 减签
     *
     * @param businessKey
     * @param assignee
     * @param removeSign
     * @return
     */
    @PostMapping(path = "/remove/sign/{businessKey}/{assignee}")
    public Response removeSign(@PathVariable("businessKey") String businessKey, @PathVariable("assignee") String assignee, @RequestBody RemoveSign removeSign) {
        return Response.success(iTaskRuntimeService.removeSign(StringUtils.hasText(removeSign.getTenantId()) ? removeSign.getTenantId() : "PMIS", businessKey, assignee, removeSign));
    }

    /**
     * 获取流程业务标识对应的当前任务的信息
     *
     * @param businessKey
     * @param assignee    当前代理人
     * @param tenantId    租户id
     * @return
     */
    @GetMapping(path = "/current/{businessKey}")
    public Response current(@PathVariable("businessKey") String businessKey, @RequestParam(required = false) String assignee, @RequestParam(required = false) String tenantId) {
        return Response.success(iTaskRuntimeService.current(tenantId, businessKey, assignee));
    }

    // 催办 urge
}
